package com.ruoyi.web.controller.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.Customer;
import com.ruoyi.market.service.CustomerService;
import com.ruoyi.system.domain.SysCity;
import com.ruoyi.system.domain.SysProvince;
import com.ruoyi.system.service.SysCityService;
import com.ruoyi.system.service.SysProvinceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 客户管理控制器
 */
@Controller
@RequestMapping("/market/customer")
public class CustomerManagementController extends BaseController {

    private String prefix="market/customer";

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SysProvinceService provinceService;

    @Autowired
    private SysCityService cityService;

    @RequiresPermissions("market:customer:view")
    @GetMapping()
    public String customer(ModelMap mmap){
        List<SysProvince> provinceList = provinceService.list();
        List<SysCity> cityList = cityService.list();
        SysUser user = getSysUser();
        mmap.put("provinceList",provinceList);
        mmap.put("cityList",cityList);
        mmap.put("user",user);
        return prefix + "/customer";
    }

    /**
     * 查询客户管理信息列表
     */
    @RequiresPermissions("market:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Customer customer, ModelMap mmap)
    {
        List<SysProvince> provinceList = provinceService.list();
        List<SysCity> cityList = cityService.list();
        SysUser user = getSysUser();
        mmap.put("user",user);
        mmap.put("provinceList",provinceList);
        mmap.put("cityList",cityList);
        startPage();
        List<Customer> list = customerService.queryList(customer);
        return getDataTable(list);
    }



    /**
     * 导出客户管理信息列表
     */
    @RequiresPermissions("market:customer:export")
    @Log(title = "客户管理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Customer customer)
    {
        List<Customer> list = customerService.queryList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        return util.exportExcel(list, "customer");
    }

    /**
     * 客户管理信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SysProvince> provinceList = provinceService.list();
        List<SysCity> cityList = cityService.list();
        mmap.put("provinceList",provinceList);
        mmap.put("cityList",cityList);
        SysUser user = getSysUser();
        mmap.put("user",user);
        return prefix + "/add";
    }

    /**
     * 新增保存客户管理信息
     */
    @RequiresPermissions("market:customer:add")
    @Log(title = "客户管理信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Customer customer)
    {
        return toAjax(customerService.save(customer));
    }

    /**
     * 分配客服人员
     */
    @GetMapping("/assign/{id}")
    public String assign(@PathVariable("id") Integer id,ModelMap mmap)
    {
        Customer customer = customerService.getById(id);
        System.out.println(customer);
        mmap.put("customer", customer);

        List<SysProvince> provinceList = provinceService.list();
        List<SysCity> cityList = cityService.list();
        mmap.put("provinceList",provinceList);
        mmap.put("cityList",cityList);
        SysUser user = getSysUser();
        mmap.put("user",user);
        return prefix + "/assign";
    }


    /**
     * 保存分配客服人员
     */
    @RequiresPermissions("market:customer:assign")
    @Log(title = "客户管理信息", businessType = BusinessType.UPDATE)
    @PostMapping("/assign")
    @ResponseBody
    public AjaxResult assignSave(Customer customer)
    {
        return toAjax(customerService.updateById(customer));
    }

    /**
     * 客户浏览详情信息
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id,ModelMap mmap)
    {
        Customer customer = customerService.getById(id);
        System.out.println(customer);
        mmap.put("customer", customer);

        List<SysProvince> provinceList = provinceService.list();
        List<SysCity> cityList = cityService.list();
        mmap.put("provinceList",provinceList);
        mmap.put("cityList",cityList);
        SysUser user = getSysUser();
        mmap.put("user",user);
        return prefix + "/detail";
    }





    /**
     * 修改客户管理信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id,ModelMap mmap)
    {
        Customer customer = customerService.getById(id);
        System.out.println(customer);
        mmap.put("customer", customer);

        List<SysProvince> provinceList = provinceService.list();
        List<SysCity> cityList = cityService.list();
        mmap.put("provinceList",provinceList);
        mmap.put("cityList",cityList);
        SysUser user = getSysUser();
        mmap.put("user",user);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户管理信息
     */
    @RequiresPermissions("market:customer:edit")
    @Log(title = "客户管理信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Customer customer)
    {
        return toAjax(customerService.updateById(customer));
    }

    /**
     * 删除客户管理信息
     */
    @RequiresPermissions("market:customer:remove")
    @Log(title = "客户管理信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(customerService.removeByIds(Arrays.asList(ids)));
    }
}
